<?php
// +----------------------------------------------------------------------
// | Longbing [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright Chengdu longbing Technology Co., Ltd.
// +----------------------------------------------------------------------
// | Website http://longbing.org/
// +----------------------------------------------------------------------
// | Sales manager: +86-13558882532 / +86-13330887474
// | Technical support: +86-15680635005
// | After-sale service: +86-17361005938
// +----------------------------------------------------------------------

declare(strict_types=1);

namespace app\survey\controller;


use app\AdminRest;
use app\agent\model\Cardauth2DefaultModel;
use app\card\model\CardCount;
use app\dynamic\model\CardauthConfig;
use app\dynamic\model\CardCompany;
use app\dynamic\model\CardShopOrder;
use app\dynamic\model\UserInfo;
use app\shop\model\AdminShopOrderGoods;
use think\App;

/**
 * @author shuixian
 * @DataTime: 2019/12/10 11:19
 * Class Admin
 * @package app\survey\controller
 */
class Admin extends AdminRest
{
    public function __construct(App $app) {
        parent::__construct($app);
    }

    //统计htmlspecialchars_encode
    public function order_statistics()
    {

        $param = $this->_param;
        //销售金额
        $company = new CardCompany();
        $order = new CardShopOrder();
        $where =[
            ['pay_status','=',1],
            ['order_status','=',3],
            ['uniacid','=',$this->_uniacid]
        ];
        if (!empty($param['company_id'])) {
            $company_list = array_unique($company->getCompanyUser_l($param['company_id']));
            $where = [
                ['a.company_id','in',$company_list],$where
            ];
        }
        $arr['day_sales_amount'] = $order->toDay($where);
        //客户数量
        $card_count = new CardCount();
        $wheres =[
            ['b.is_staff','=',1],
            ['sign','=','praise'],
            ['type','=',2],
            ['a.uniacid','=',$this->_uniacid]
        ];
        if (!empty($param['company_id'])) {
            $wheres = [
                ['b.company_id','in',$company_list],$wheres
            ];
        }
        $today = $card_count->getCountUser($wheres,'today');
        $yesterday = $card_count->getCountUser($wheres,'yesterday');
        if ($yesterday == 0) {
            $proportion = 0;
        } else {
            $proportion = $today / $yesterday * 100;
        }
        $up = $today>=$yesterday?1:0;
        $arr['today_user'] = ['user_today' => $today,'up'=>$up ,'user_proportion' => round($proportion, 2)];
        //员工数量
        $user_info = new UserInfo();
        if (!empty($data['company_id'])) {
            $where1['company_id'] = $data['company_id'];
        }
        $where1['b.is_staff'] = 1;
        $where1['b.uniacid'] = $this->_uniacid;
        $where1['a.uniacid'] = $this->_uniacid;
        //设置为名片的人数
        $where1['b.status'] = 1;
        $where1['a.is_staff'] = 1;
        $where1['b.deleted'] = 0;
        $arr['surplus_staff_user'] = $user_info->userStaff($where1);
        //部门的统计
        $where3['a.uniacid'] = $this->_uniacid;
        $where3['a.status'] = 1;

        if (empty($param['company_id'])) {
            $arr['job_user'] = $company->getCompanyUserNumber($where3);
        } else {
            $arr['job_user'] = $company->getJobUser($param['company_id'], $this->_uniacid);
        }
        //未设置为名片的人数
        // $where1['status'] = -1;CardauthConfig

        //By.jingshuixian
        //$card_number = LONGBING_AUTH_CARD;
        $permissionCard = new \app\card\info\PermissionCard($this->_uniacid) ;
        $card_number = $permissionCard->getAuthNumber() ;

        $bb = true;
        if($card_number==0){
            $config = new CardauthConfig();
            $aa = true;
            if($config->is_table()){
                $card = $config->getinfo(['modular_id'=>$this->_uniacid]);
                if($card){
                    if($card['number']>0){
                        $card_number = $card['number'];
                    }else{
                        $bb = false;
                        $card_number = '无限制';
                    }
                }else{
                    $aa = false;
                }
            }else{
                $aa = false;
            }
            if($aa ==false){
                $card_auth = new Cardauth2DefaultModel();
                if($card_auth->is_table()){
                    $card = $card_auth->getinfo();
                    if(empty($card)){
                        $card_number  = 5;
                    }else{
                        if($card['card_number']>0){
                            $card_number = $card['card_number'];
                        }else{
                            $bb = false;
                            $card_number = '无限制';
                        }
                    }
                }else{
                    $bb = false;
                    $card_number = '请安装代理端';
                }
            }
        }
        if($bb == false){
            $arr['staff_user'] = $card_number;
        }else{
            $card_number = $card_number-$arr['surplus_staff_user'];
            $arr['staff_user'] = is_numeric($card_number)&&$card_number<0?0:$card_number;//$user_info->userStaff($where1);
        }
        //销量排行
        $admin_goods = new AdminShopOrderGoods();
        $where2 =[
            ['a.status','=',1],
            ['a.uniacid','=',$this->_uniacid]
        ];
        if (!empty($param['company_id'])) {
            $where2 = [
                ['b.company_id','in',$company_list],$where2
            ];
        }
        $arr['sales_count'] = $admin_goods->getGoosNember($where2);
        //销售业绩
        /*$time = '';
        if (!empty($param['time'])) {
            if ($param['time'] == 7) {
                $time = '-7 day';
            } elseif ($param['time'] == 15) {
                $time = '-15 day';
            } elseif ($param['time'] == 30) {
                $time = '-30 day';
            }
        }
        if (empty($param['company_id'])) {
            $where4['a.uniacid'] = $this->_uniacid;
            $where4['a.pid'] = 0;
            $arr['sales_sum'] = $company->getOrderSum($where4,$time);
        } else {
            $company_list2 = array_unique($company->getCompanyUser_top($param['company_id']));
            $where3 = [
                ['a.id', 'in', $company_list2],
                ['a.uniacid', '=', $this->_uniacid]
            ];
            $arr['sales_sum'] = $company->getOrderSum($where3, $time);
        }*/

        return $this->success($arr);
    }
}